package com.yami.shop.search.api.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.yami.shop.bean.model.UserSearchHistory;
import com.yami.shop.common.bean.SensitiveWord;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.search.common.util.EsSearchHotWordUtil;
import com.yami.shop.service.SysConfigService;
import com.yami.shop.service.UserSearchHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 商品搜索
 * @author FrozenWatermelon
 * @date 2020/11/16
 */
@RestController("hotwordsSearchController")
@RequestMapping("/hotSearch")
@Tag(name = "商品搜索接口")
public class HotwordsSearchController {
    @Autowired
    private UserSearchHistoryService userSearchHistoryService;
    @Autowired
    private SysConfigService sysConfigService;
    @GetMapping("/searchByPrefix")
    @Operation(summary =  "自动补全搜索接口", description = "搜索接口")
    public ServerResponseEntity<List<String>> searchByPrefix(String prefix,Integer size) {
        size = Objects.isNull(size)?5:size;
        return ServerResponseEntity.success(EsSearchHotWordUtil.suggestions(prefix,size));
    }

    @GetMapping("/searchByUserId")
    @Operation(summary =  "用户搜索接口", description = "搜索接口")
    public ServerResponseEntity<List<String>> searchByUserId(String userId,Integer size) {
        size = Objects.isNull(size)?5:size;
        return ServerResponseEntity.success(EsSearchHotWordUtil.searchByUserId(userId,size));
    }

    @GetMapping("/searchByUserIdOrPrefix")
    @Operation(summary =  "搜索接口", description = "搜索接口")
    public ServerResponseEntity searchByUserIdOrPrefix(String prefix,String userId,Integer size) {
        size = Objects.isNull(size)?5:size;
        List<String> res = new ArrayList<String>();
        if(StrUtil.isNotBlank(prefix)){
            res = EsSearchHotWordUtil.suggestions(prefix.trim(), size);
        }else if(StrUtil.isNotBlank(userId)){
            res = EsSearchHotWordUtil.searchByUserId(userId,size);
            return ServerResponseEntity.success();
        }
        if(res.size()>0){
            SensitiveWord sensitiveWord = sysConfigService.getSysConfigObject(Constant.SENSITIVE_WORDS, SensitiveWord.class);
            //根据中文逗号分隔
            List<String> sensitiveWordsList = Arrays.asList(sensitiveWord.getSensitiveWord().split(Constant.CN_COMMA));
            HashSet<String> sensitiveWordSet = new HashSet<>(sensitiveWordsList);
            Iterator<String> iterator = res.iterator();
            while (iterator.hasNext()) {
                if(sensitiveWordSet.contains(iterator.next())){
                    iterator.remove();
                    continue;
                }
            }
        }
        return ServerResponseEntity.success(res);
    }

    @GetMapping("/createIndex")
    @Operation(summary =  "热搜搜索接口（仅热搜信息）", description = "搜索接口")
    public ServerResponseEntity createIndex(String hotwords) {
        EsSearchHotWordUtil.createIndex(hotwords);
        return ServerResponseEntity.success();
    }
    @GetMapping("/save")
    @Operation(summary =  "添加记录", description = "添加记录接口")
    public ServerResponseEntity save(UserSearchHistory userSearchHistory) {
        userSearchHistory.setCreateTime(new Date());
        EsSearchHotWordUtil.save(userSearchHistory);
        return ServerResponseEntity.success();
    }
    @GetMapping("/saveAll")
    @Operation(summary =  "热搜数据备份到es", description = "搜索接口")
    public ServerResponseEntity saveAll() {
        List<UserSearchHistory> list = userSearchHistoryService.list();
        for (UserSearchHistory userSearchHistory:list) {
            EsSearchHotWordUtil.save(userSearchHistory);
        }
        return ServerResponseEntity.success();
    }
}
